{% extends 'base.html' %}
{% block content %}
<div class="container mt-4">
  <h2>{{ post.title }}</h2>
  {% if post.is_approved == False and current_user.is_authenticated and current_user.id == post.author_id %}
  <div class="alert alert-danger">
      <h5>帖子未通过审核</h5>
      <p>拒绝原因：{% if post.rejection_reason %}{{ post.rejection_reason }}{% else %}无具体原因{% endif %}</p>
      <p>请根据反馈修改后重新提交</p>
  </div>
  {% endif %}
  <p class="text-muted">作者：{{ post.author.username or post.author.email }} | {{ post.created_at.strftime('%Y-%m-%d %H:%M') }}</p>
  <div class="mb-4">{{ post.content|safe }}</div>

  <!-- 附件处理 -->
  {% if post.attachment %}
  <div class="mb-4">
    <h5>附件</h5>
    {% set ext = post.attachment.split('.')[-1].lower() %}
    
    {# 图片预览 #}
    {% if ext in ['jpg', 'jpeg', 'png', 'gif'] %}
      <div class="mb-2">
        <img src="{{ url_for('download_file', filename=post.attachment) }}" 
             class="img-thumbnail" 
             style="max-height: 300px; max-width: 100%;"
             alt="附件预览">
      </div>
    {% endif %}
    
    {# PDF预览链接 #}
    {% if ext == 'pdf' %}
      <div class="mb-2">
        <a href="{{ url_for('static', filename='lib/pdfjs/web/viewer.html') }}?file={{ url_for('download_file', filename=post.attachment, _external=True) }}" 
           target="_blank" 
           class="btn btn-info">
          <i class="bi bi-eye"></i> 预览PDF
        </a>
      </div>
    {% endif %}
    
    {# 下载按钮 #}
    <a href="{{ url_for('download_file', filename=post.attachment) }}" 
       class="btn btn-primary">
      <i class="bi bi-download"></i> 下载附件
    </a>
  </div>
  {% endif %}

  <!-- 三连互动区域 -->
  <div class="d-flex justify-content-between mb-4">
  <form method="post" action="{{ url_for('post_interact', post_id=post.id) }}">
      <input type="hidden" name="action" value="like">
      <button type="submit" class="btn btn-outline-primary">
        <i class="bi bi-hand-thumbs-up{% if interaction and interaction.liked %}-fill{% endif %}"></i>
        点赞 {% if post.get_like_count() > 0 %}({{ post.get_like_count() }}){% endif %}
      </button>
    </form>
    
    <form method="post" action="{{ url_for('post_interact', post_id=post.id) }}">
      <input type="hidden" name="action" value="coin">
      <button type="submit" class="btn btn-outline-warning">
        <i class="bi bi-coin{% if interaction and interaction.coined %}-fill{% endif %}"></i>
        投币 {% if post.get_coin_count() > 0 %}({{ post.get_coin_count() }}){% endif %}
      </button>
    </form>
    
    <form method="post" action="{{ url_for('post_interact', post_id=post.id) }}">
      <input type="hidden" name="action" value="favorite">
      <button type="submit" class="btn btn-outline-success">
        <i class="bi bi-bookmark{% if interaction and interaction.favorited %}-fill{% endif %}"></i>
        收藏 {% if post.get_favorite_count() > 0 %}({{ post.get_favorite_count() }}){% endif %}
      </button>
    </form>
  </div>
  <hr>
  <h5>分享</h5>
  <div class="mb-2">
    <input type="text" class="form-control" value="{{ share_url }}" readonly>
    <p>如果需要分享，那请将上方链接全选后复制并发送给别人</p>
  </div>
  <div class="mb-2">
    <textarea class="form-control" rows="2" readonly>{{ share_text }}</textarea>
  </div>
  <div class="mb-2">
    <img src="data:image/png;base64,{{ img_b64 }}" alt="二维码" style="width:150px;">
    <p>右键二维码可下载<from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class Collection(Base):
    __tablename__ = '收藏夹'
    id = Column(Integer, primary_key=True)
    name = Column(String)

# 创建数据库引擎
engine = create_engine('sqlite:///yourdatabase.db')

# 创建表
Base.metadata.create_all(engine)

# 创建会话
Session = sessionmaker(bind=engine)
session = Session()
/p>
    <p>以Microsoft Edge为例</p>
    <hr>
    <a href="https://gitee.com/anyway-its-just-fi0m9ysans/letter-pack-issue-tracker/blob/master/%E5%85%B6%E4%BB%96/Microsoft%20Edge%E4%B8%8B%E8%BD%BD%E4%BA%8C%E7%BB%B4%E7%A0%81%E6%95%99%E7%A8%8B">访问二维码下载教程</a>
    <hr>
  </div>
  <div class="d-flex gap-2">
    <a href="{{ url_for('board') }}" class="btn btn-secondary">返回讨论板</a>
    {% if current_user.is_authenticated %}
      <form method="post" action="{{ url_for('toggle_favorite', post_id=post.id) }}">
        <button type="submit" class="btn {{ 'btn-danger' if is_favorited else 'btn-outline-danger' }}">
          {{ '取消收藏' if is_favorited else '收藏' }}
        </button>
      </form>
      {% if current_user.id == post.author_id %}
        <form method="post" action="{{ url_for('delete_post', post_id=post.id) }}" 
              onsubmit="return confirm('确定要删除这篇帖子吗？此操作无法撤销。');">
          <button type="submit" class="btn btn-danger">删除帖子</button>
        </form>
      {% endif %}
    {% endif %}
  </div>

  <!-- 评论功能 -->
  <hr>
  <h5>评论</h5>
  {% if current_user.is_authenticated %}
    <form method="post" class="mb-3">
      <div class="mb-3">
        <textarea class="form-control" name="comment" rows="3" required placeholder="输入评论..."></textarea>
      </div>
      <button type="submit" class="btn btn-primary">提交评论</button>
    </form>
  {% else %}
    <div class="alert alert-info">
      请<a href="{{ url_for('login') }}">登录</a>后发表评论
    </div>
  {% endif %}
  <div class="comments-list">
    {% if comments %}
      {% for comment in comments %}
        <div class="card mb-3">
          <div class="card-body">
            <div class="d-flex justify-content-between">
              <h6 class="card-title">{{ comment.author.username or comment.author.email }}</h6>
              <small class="text-muted">{{ comment.created_at.strftime('%Y-%m-%d %H:%M') }}</small>
            </div>
            <p class="card-text">{{ comment.content }}</p>
            {% if current_user.is_authenticated and (current_user.id == comment.author_id or current_user.is_admin) %}
            <div class="mt-2">
              <form method="post" action="{{ url_for('delete_comment', comment_id=comment.id) }}" 
                    onsubmit="return confirm('确定要删除这条评论吗？此操作无法撤销。');">
                <button type="submit" class="btn btn-danger btn-sm">删除</button>
              </form>
            </div>
            {% endif %}
          </div>
        </div>
      {% endfor %}
    {% else %}
      <div class="alert alert-secondary">暂无评论</div>
    {% endif %}
  </div>
  <hr>
  <h5>相关帖子(最多5个)(暂未实现)</h5>
  <ul class="list-group">
    {% for related_post in related_posts %}
      <li class="list-group-item">
        <a href="{{ url_for('post_detail', post_id=related_post.id) }}">{{ related_post.title }}</a>
        <span class="text-muted float-end">{{ related_post.created_at.strftime('%Y-%m-%d %H:%M') }}</span>
      </li>
    {% endfor %}
  </ul>
  {% if not related_posts %}
    <p>暂无相关帖子。(点击这里登录后查看)</p>
  {% endif %}

  <!-- 添加文件下载链接 -->
  {% if post.file %}
  <div class="mb-4">
    <h5>附件</h5>
    <a href="{{ url_for('download', file_id=post.file.id) }}" class="btn btn-primary">下载文件</a>
  </div>
  {% endif %}
</div>
{% endblock %}